package com.banyou.banyou.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.banyou.banyou.MainApplication;
import com.banyou.banyou.R;
import com.banyou.banyou.util.MyActivityManager;
import com.banyou.banyou.util.ShowToast;
import com.banyou.banyou.webutils.ProgressSubscriber;

import java.text.SimpleDateFormat;


/**
 * ----------BigGod be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神兽出没━━━━━━
 * <p/>
 * 项目名称：My Application
 * 包名：com.yztc.a1602.myapplication.base
 * 类描述：所有Activity的父类
 * 创建人:luxin
 * 创建时间：2016-08-14
 * 修改人：luxin
 * 修改时间：2016-08-14
 * 修改备注：
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {
    protected P mPresenter;
    protected AlertDialog mDialog;//弹出窗口
    protected Window mWindowDialog;//获取Dialog的window
    private static final int TEXT = 1, REPORT = 2;
    protected String mToken, mSignCode;
    private SimpleDateFormat mFormat = new SimpleDateFormat("MM月dd日 hh:mm:ss");
    public View mLayoutView;
    public int mLayoutViewId;
    private AlertDialog mReportDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyActivityManager.getInstance().pushOneActivity(this);//存入MyActivityManager
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);//共享元素必备
        if (initLayout() != -1) {   //若传入LayoutId是-1，则判断传入的是View
            mLayoutViewId = initLayout();
            mLayoutView = LayoutInflater.from(this).inflate(mLayoutViewId, null);
            setContentView(mLayoutView);
        } else {
            mLayoutView = initLayoutView();
            setContentView(mLayoutView);
        }
        mPresenter = initPresenter();  //初始化Presenter
        SharedPreferences preferences = getSharedPreferences("login"
                , Activity.MODE_PRIVATE);   //全局Token获取
        mToken = preferences.getString("token", "");

        init(); //初始化
        init(savedInstanceState);
    }

    public void init(Bundle savedInstanceState) {

    }


    protected abstract P initPresenter();

    /**
     * 设置LayoutID
     *
     * @return LayoutID
     */
    protected abstract int initLayout();

    /**
     * 设置LayoutVIew
     *
     * @return
     */
    protected abstract View initLayoutView();

    /**
     * 所有BaseActivity的init方法
     */
    protected abstract void init();

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.destoryPresenter();
            mPresenter = null;
        }
        MyActivityManager.getInstance().removeOnActivityFromStack(this);
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
//        unregisterReceiver(pushReceiver);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    protected abstract String pageName();

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 启动一个Activity
     *
     * @param activity 要启动的activity的class
     */
    public void startActivity(Class<? extends Activity> activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    private AlertDialog mLoadingDialog;
    public final int LOADING_WHITE=0x888,LOADING_BLACK=0x999;
    public void showLoadingDialog(int style) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        View view = LayoutInflater.from(this).inflate(R.layout.loading_logo_dialog, null);//dialog的自定义按钮
        ImageView imageView = (ImageView) view.findViewById(R.id.loading_logo);
        if(style==LOADING_BLACK){
            imageView.setImageResource(R.drawable.loading_black);
        }else {
            imageView.setImageResource(R.drawable.loading_white);
        }
        Animation animation = AnimationUtils.loadAnimation(MainApplication.getContext(), R.anim.logo_rotation);
        imageView.setAnimation(animation);
        animation.start();
        builder.setView(view);
        mLoadingDialog = builder.create();
        Window windowDialog = mLoadingDialog.getWindow();//获取Window，以便加载动画效果
        windowDialog.setBackgroundDrawableResource(R.drawable.alert_dialog_shap);//设置dialog的透明
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public void requestFail(Throwable e,String error){
        if(e instanceof ProgressSubscriber.ReturnCodeErrorException){
            ShowToast.showToast(((ProgressSubscriber.ReturnCodeErrorException) e).getMsg());
        }else{
            ShowToast.showToast(error);
        }
    }

    public SharedPreferences getSharedPreference(){
        SharedPreferences preferences=getSharedPreferences("login",MODE_PRIVATE);
        return preferences;
    }
}
